package com.starbaba.system.syscode.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.starbaba.common.HttpConnection;
import com.starbaba.core.web.constant.Constants;
import com.starbaba.core.web.translate.TranslateUtils;
import com.starbaba.core.web.translate.TranslateUtilsFactory;
import com.starbaba.core.web.translate.TranslateVO;
import com.starbaba.model.network.Phead;
import com.starbaba.system.syscode.model.SyscodeVO;
import com.starbaba.system.syscode.service.SyscodeService;

@SuppressWarnings("all")
public class LifeConstant {

	// 商家类型
	public final static String BUSI_PARKING = "1";

	public final static String BUSI_GAS = "2";

	// ////////////////////////////////////////////////////////////////
	// 图片类型
	public final static String IMG_BUSI = "1";

	public final static String IMG_PRICE = "3";

	public final static String IMG_PROMOTION = "4";

	// ///////////////////////////////////////////////////////////////

	private final static HashMap<String, List<SyscodeVO>> MAP = new HashMap<String, List<SyscodeVO>>();
	

	/**
	 * 获取停车场类型
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<SyscodeVO> getParkingType() throws Exception {
		List<SyscodeVO> list = MAP.get("carparktype");
		if (list == null) {
			TranslateUtils instance = TranslateUtilsFactory.getInstance();
			SyscodeService impl = instance.getSyscodeService();
			list = impl.doFindSyscodeVOByType("carparktype", new int[] { -1,
					104 });
			MAP.put("carparktype", list);
		}
		return MAP.get("carparktype");
	}

	/**
	 * 获取停车场车位数类型
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<SyscodeVO> getParkingSpaceType() throws Exception {
		List<SyscodeVO> list = MAP.get("parkspacetype");
		if (list == null) {
			TranslateUtils instance = TranslateUtilsFactory.getInstance();
			SyscodeService impl = instance.getSyscodeService();
			list = impl
					.doFindSyscodeVOByType("parkspacetype", new int[] { -1 });
			MAP.put("parkspacetype", list);
		}
		return MAP.get("parkspacetype");
	}

	/**
	 * 获取贡献的错误类型
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<SyscodeVO> getContributionErrorType() throws Exception {
		List<SyscodeVO> list = MAP.get("PARK_REPORT_ERRTYPE");
		if (list == null) {
			TranslateUtils instance = TranslateUtilsFactory.getInstance();
			SyscodeService impl = instance.getSyscodeService();
			list = impl.doFindSyscodeVOByType("PARK_REPORT_ERRTYPE",
					new int[] { -1 });
			MAP.put("PARK_REPORT_ERRTYPE", list);
		}
		return MAP.get("PARK_REPORT_ERRTYPE");
	}

	public static String getContributionErrorType(int code) throws Exception {
		List<SyscodeVO> contributionErrorType = getContributionErrorType();
		for (SyscodeVO syscodeVO : contributionErrorType) {
			if (syscodeVO.getCode().intValue() == code) {
				return syscodeVO.getKindname();
			}
		}
		return "unknow";
	}

	/**
	 * 获取"有无"的字典
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<SyscodeVO> getHasOrNot() throws Exception {
		List<SyscodeVO> list = MAP.get("HASORNOT");
		if (list == null) {
			TranslateUtils instance = TranslateUtilsFactory.getInstance();
			SyscodeService impl = instance.getSyscodeService();
			list = impl.doFindSyscodeVOByType("HASORNOT", new int[] { -1 });
			MAP.put("HASORNOT", list);
		}
		return MAP.get("HASORNOT");
	}

	/**
	 * 获取分数的字典
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<SyscodeVO> getStar() throws Exception {
		List<SyscodeVO> list = MAP.get("COMMENTSTAR");
		if (list == null) {
			TranslateUtils instance = TranslateUtilsFactory.getInstance();
			SyscodeService impl = instance.getSyscodeService();
			list = impl.doFindSyscodeVOByType("COMMENTSTAR", new int[] { -1 });
			MAP.put("COMMENTSTAR", list);
		}
		return MAP.get("COMMENTSTAR");
	}

	/**
	 * 获取审核状态
	 * @return
	 * @throws Exception
	 */
	public static List<SyscodeVO> getAuditStatus() throws Exception {
		List<SyscodeVO> list = MAP.get("CONTRIBUTE_STATUS");
		if (list == null) {
			TranslateUtils instance = TranslateUtilsFactory.getInstance();
			SyscodeService impl = instance.getSyscodeService();
			list = impl.doFindSyscodeVOByType("CONTRIBUTE_STATUS",
					new int[] { -1 });
			MAP.put("CONTRIBUTE_STATUS", list);
		}
		return MAP.get("CONTRIBUTE_STATUS");
	}

	/**
	 * 
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public static List<SyscodeVO> getSysCode(String type) throws Exception {
		List<SyscodeVO> list = MAP.get(type);
		if (list == null) {
			TranslateUtils instance = TranslateUtilsFactory.getInstance();
			SyscodeService impl = instance.getSyscodeService();
			if("carparktype".equals(type)){
				list = getParkingType();
			}else{
				list = impl.doFindSyscodeVOByType(type, new int[] { -1 });
			}
			MAP.put(type, list);
		}
		return MAP.get(type);
	}
	
	public static List<TranslateVO> getSysCodeTranslate(String type) throws Exception {
		List<SyscodeVO> list = MAP.get(type);
		if (list == null) {
			list = getSysCode(type);
			MAP.put(type, list);
		}
		list = MAP.get(type);
		ArrayList<TranslateVO> translateList = new ArrayList<TranslateVO>();
		for (SyscodeVO syscodeVO : list) {
			translateList.add(new TranslateVO(syscodeVO.getCode().toString(), syscodeVO
					.getKindname()));
		}
		return translateList;
	}

	/**
	 * 获取用户名
	 * 
	 * @param userId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String getUserName(String userId)
			throws ClientProtocolException, IOException {
		String name = "unknow";
		DefaultHttpClient httpClient = new DefaultHttpClient();
		JsonObject obj = new JsonObject();
		JsonElement pheadjson = new Gson().toJsonTree(new Phead());
		obj.add("phead", pheadjson);
		obj.addProperty("userid", userId);
		String url = Constants.BASE_URL
				+ "/user_service/common?funid=5&shandle=1&rd="
				+ System.currentTimeMillis();
		ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("data", obj.toString()));
		String content = HttpConnection.connectionZip(url, HttpConnection.POST,
				list);
		JsonObject parse = (JsonObject) new JsonParser().parse(content);
		JsonObject result = parse.getAsJsonObject("result");
		JsonPrimitive status = result.getAsJsonPrimitive("status");
		if (status.getAsInt() == 1) {
			JsonObject userinfo = parse.getAsJsonObject("userinfo");
			if (userinfo != null) {
				name = userinfo.getAsJsonPrimitive("petname").getAsString();
			}
		}
		httpClient.getConnectionManager().shutdown();
		return name;
	}

}
